#include "imageBMP.h"
#include "Grille.h"
#include <iostream>
using namespace std;

Grille::Grille(void){}
Grille::Grille(imageBMP *_image){
	image = _image;
	init();
}
void Grille::init(void){
	height = image->getHeight();
	width = image->getWidth();
	
	tab = new int*[height];
	for(int i=0; i<height; i++)
		tab[i] = new int[width];
	for(int i=0; i<height; i++){
		for(int j=0; j<width; j++){
			//printf("%d\n",image->getPixel(i,j));
			if(image->getPixel(i,j)==-1){
				tab[i][j]=0;
			}else if(image->getPixel(i,j)==120){
				tab[i][j]=2;
			}else if(image->getPixel(i,j)==80){
				tab[i][j]=5;
			}else{
				tab[i][j]=1;
			}
		}
	}
}
void Grille::refresh(void){
	init();
}
Grille::~Grille(void){
	for(int i=0; i<height; i++)
		delete[] tab[i];
	delete[] tab;
}
void Grille::afficheGrille(void){
	for(int y=0; y<height; y++){
		for(int x=0; x<width; x++){	
			if(tab[x][y]==1){
				printf("##");
			}else if(tab[x][y]==2){
				printf(". ");
			}else if(tab[x][y]==3){
				printf("O ");
			}else{
				printf("  ");
			}
		}
		printf("\n");
	}
	return;
}
bool Grille::isFill(int x, int y){
	if(x<0 || x>=width || y<0 || y>=height){
		return false;
	}
	if(tab[x][y]==1){
		return true;
	}else{
		return false;
	}
}
int Grille::get(int x, int y){
	if(x<0 || x>=width || y<0 || y>=height){
		return -1;
	}
	return tab[x][y];
}
void Grille::set(int x, int y, int value){
	if(x<0 || x>=width || y<0 || y>=height){
		cout << "Erreur : Modification d'une case en dehors de la grille" << endl;
	}else{
		tab[x][y]=value;
	}
}
int Grille::getWidth(){
	return width;
}
int Grille::getHeight(){
	return height;
}

void Grille::transporter(float *x, float *y){
	cout << endl << "("<<*x<<","<<*y<<") -> " << getWidth()-1 <<endl;
	if((int)*x == 1)
		*x = getWidth()-2;
	else if((int)*x == getWidth()-2)
		*x = 1;
	
	if((int)*y == 1)
		*y = getHeight()-1;
	else if((int)*y == getHeight()-1)
		*y = 1;
}